﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebFrontEnd.Helpers;

namespace WebFrontEnd
{
    public partial class RSVP : PageBase
    {
        int invID;
        protected void Page_Load(object sender, EventArgs e)
        {
            int gid;
            string h = Request.QueryString["h"];
            if (!int.TryParse(Request.QueryString["gid"], out gid) || h == null)
            {
                Response.Clear();
                Response.StatusCode = 404;
                Server.Transfer("ErrorPages/404.aspx");
            }
            var inv = BL.GetInviteByHash(h, gid);
            if (inv == null)
            {
                Response.Clear();
                Response.StatusCode = 404;
                Server.Transfer("ErrorPages/404.aspx");
            }
            else if (inv.RSVPd)
            {
                realBody.InnerHtml = "";
                fakebody.InnerHtml = "You have already RSVPd for " + inv.Event.Name + ", thank you!";
                modalFooter.InnerHtml = "";
            }
            else
            {
                nameLabel.Text = inv.Event.Name;
                invID = inv.ID;
                rsvpGuestsInput.Attributes["min"] = "0";
                rsvpGuestsInput.Attributes["max"] = (inv.AdditionalInvitedGuests + (inv.Guest.Couple ? 2 : 1)).ToString();
                guestNameLabel.Text = inv.Guest.CombinedName();
            }

        }

        protected void updateRsvpButton_Click(object sender, EventArgs e)
        {
            var inviteToUpdate = BL.GetInviteById(invID);
            inviteToUpdate.RSVPd = true;
            inviteToUpdate.ReservedGuests = int.Parse(rsvpGuestsInput.Value);
            BL.UpdateInvite(inviteToUpdate);
            realBody.InnerHtml = "";
            fakebody.InnerHtml = "Thank you for RSVPing for " + inviteToUpdate.Event.Name + "!";
            modalFooter.InnerHtml = "";
        }


    }
}